package com.blue.http.controller;

import com.blue.core.dict.HttpMethod;
import com.blue.http.annotation.Http;
import com.blue.http.config.ErrorConfig;
import com.blue.http.message.AccessToken;
import com.blue.http.message.HttpRequest;
import com.blue.validation.group.SaveModel;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller
@Http(url = "/token", method = HttpMethod.GET)
public class TokenController
{
	@Resource
	private Authorized authorized;

	private ErrorConfig errorConfig = ErrorConfig.getInstance();

	public TokenController()
	{
	}

	public AccessToken token(HttpRequest request)
	{
		System.out.printf("客户端IP：%s\n", request.getIp());
		Account account = request.getQueryObject(Account.class, SaveModel.class);
		String token = authorized.login(account.getName(), account.getPassword());
		if (token == null || token.isEmpty())
			throw errorConfig.getException(ErrorConfig.LOGIN);

		return new AccessToken(token);
	}

}

